package com.example.ssm.controller;

import com.example.ssm.entity.Bill;
import com.example.ssm.entity.Menu;
import com.example.ssm.entity.Merchant;
import com.example.ssm.entity.User;
import com.example.ssm.service.BillService;
import com.example.ssm.service.MenuService;
import com.example.ssm.service.ShopService;
import com.example.ssm.service.impl.BillServiceImpl;
import com.example.ssm.service.impl.MenuServiceImpl;
import com.example.ssm.service.impl.ShopServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/Menu")
public class MenuController {
    @RequestMapping("/addMenu")
    public String addMenu(Menu menu, Model model, String user_name,HttpServletRequest req) throws IOException {
        MenuService service=new MenuServiceImpl();
        System.out.println(menu);
        System.out.println(user_name);
        ShopService shopService=new ShopServiceImpl();
        menu.setShop_name(shopService.select(user_name).getShop_name());
        service.insertMennu(menu);
        List<Menu> menus;
        menu.setShop_name(shopService.select(user_name).getShop_name());
        menus=service.selectMenuByShopName(menu.getShop_name());

        BillService billService=new BillServiceImpl();
        Merchant merchant;
        User user=new User();
        user.setUname(user_name);
        merchant=shopService.select(user_name);

        List<Bill>NOPayBill= billService.selectNoPayByshop_name(merchant);
        List<Bill>PayedBill= billService.selectPayByshop_name(merchant);
        model.addAttribute("NOPayBill",NOPayBill);
        model.addAttribute("PayedBill",PayedBill);
        double value=0;
        int j=0;
        while (j<NOPayBill.size() ){
            double price= NOPayBill.get(j).getPrice();
            value+=price;
            j++;
        }
        model.addAttribute("value",value);

        double value1=0;
        int k=0;
        while (k<PayedBill.size() ){
            double price= PayedBill.get(k).getPrice();
            value1+=price;
            k++;
        }
        model.addAttribute("value1",value1);
        req.setAttribute("menus",menus);
        model.addAttribute("menus",menus);
        return "Merchantback";
    }
    @RequestMapping("/delMenu")
    public String delMenu(Menu menu, HttpServletRequest req,String user_name,Model model) throws IOException {
        MenuService service=new MenuServiceImpl();
        service.delMenu(menu);

        ShopService shopService=new ShopServiceImpl();
        Merchant merchant=new Merchant();
        merchant=shopService.select(user_name);
        Menu menu1=new Menu();
        menu1.setShop_name(merchant.getShop_name());
        List<Menu> menus=new  ArrayList<>();
        menus=  service.selectMenuByShopName(menu1.getShop_name());//店名
        req.setAttribute("list",menus);
        model.addAttribute("menus",menus);


        BillService billService=new BillServiceImpl();
        List<Bill> NOPayBill= new ArrayList<Bill>();
        NOPayBill=  billService.selectNoPayByshop_name(merchant);
        List<Bill> PayedBill= new ArrayList<Bill>();
        PayedBill= billService.selectPayByshop_name(merchant);
        double value=0;
        int j=0;
        while (j<NOPayBill.size() ){
            double price= NOPayBill.get(j).getPrice();
            value+=price;
            j++;
        }
        model.addAttribute("value",value);

        double value1=0;
        int k=0;
        while (k<PayedBill.size() ){
            double price= PayedBill.get(k).getPrice();
            value1+=price;
            k++;
        }
        model.addAttribute("value1",value1);
        model.addAttribute("NOPayBill",NOPayBill);
        model.addAttribute("PayedBill",PayedBill);


        return "Merchantback";
    }
    @RequestMapping("/search")
    public String search(HttpServletRequest req, Menu menut,Model model,String myname) throws IOException {
        MenuService menuService=new MenuServiceImpl();
        List<Menu> menu= new ArrayList<>();
        User user=new User();
        user.setUname(myname);
        System.out.println(menut);
           if(!menut.getName().equals("")&&!menut.getShop_name().equals("")&&!menut.getPrice().equals("")&&!menut.getType().equals("")){
               menu=menuService.selectType(menut);
               System.out.println("shop_name+name+price");
           }
        else  if(menut.getName().equals("")&&!menut.getShop_name().equals("")&&!menut.getPrice().equals("")&&!menut.getType().equals("")){
               menu=menuService.selectTypeppriceshop_name(menut);
               System.out.println("shop_name+price");
           }
        else  if(!menut.getName().equals("")&&menut.getShop_name().equals("")&&!menut.getPrice().equals("")&&!menut.getType().equals("")){
               menu=menuService.selectTypeppricename(menut);
               System.out.println("name+price");
           }
        else  if(!menut.getName().equals("")&&!menut.getShop_name().equals("")&&menut.getPrice().equals("")&&!menut.getType().equals("")){
               menu=menuService.selectTypepshop_namename(menut);
               System.out.println("shop_name+name");
           }
           else  if(menut.getName().equals("")&&menut.getShop_name().equals("")&&!menut.getPrice().equals("")&&!menut.getType().equals("")){
               menu=menuService.selectTypeprice(menut);
               System.out.println("price");
           }
           else  if(menut.getName().equals("")&&!menut.getShop_name().equals("")&&menut.getPrice().equals("")&&!menut.getType().equals("")){
               menu=menuService.selectTypeshop_name(menut);
               System.out.println("shop_name");
           }
           else  if(!menut.getName().equals("")&&menut.getShop_name().equals("")&&menut.getPrice().equals("")&&!menut.getType().equals("")){
               menu=menuService.selectTypeName(menut);
               System.out.println("name");
           }
           else  if(menut.getName().equals("")&&menut.getShop_name().equals("")&&menut.getPrice().equals("")&&!menut.getType().equals("")){
               menu=menuService.selectType(menut);
               System.out.println("type");

           }
        System.out.println(menu);
        req.setAttribute("list",menu);
        model.addAttribute("menus",menu);
        return "search";
    }
}
